﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace AIS.SDK.Api
{
    public abstract class ApiResponse
    {
        public bool IsError
        {
            get
            {
                return ErrorCode != 0;
            }
        }

        

        [JsonProperty("errcode")]
        public int ErrorCode { get; set; }

        [JsonProperty("message")]
        public string ErrorMessage { get; set; }

        public bool ok { get; set; }
        public override string ToString()
        {
            if (IsError)
            {
                return String.Format("errcode:{0}, errmsg:{1}", exception, ErrorMessage);
            }

            return base.ToString();
        }
         
        /// <summary>
        /// 
        /// </summary>
        public string exception { get; set; }
        /// <summary>
        /// 
        /// </summary>
        public string timeStamp { get; set; }
        public string JsonString { get; set; }

        public object data { get; set; }

        public T GetDataObject<T>()
        {
            T obj= Common.JsonHelper.JsonToObject<T>(data.ToString());
            System.Type t = typeof(T);
            Assembly ass = Assembly.GetAssembly(t);//获取泛型的程序集
            PropertyInfo[] pc = t.GetProperties();//获取到泛型所有属性的集合
 
            foreach (PropertyInfo pi in pc)
            {
                if(pi.Name.ToLower().Equals("jsonstring"))
                    pi.SetValue((T)obj, data.ToString(), null);//给泛型的属性赋值
                if (pi.Name.ToLower().Equals("ok"))
                    pi.SetValue((T)obj, ok, null);//给泛型的属性赋值
            }
             
            return obj;
        }

        


    }
}
